package View.BreakRule;

import Service.BreakRuleService;
import Service.ServiceImpl.BreakRuleServiceImpl;
import View.Index.Index_admin;
import domain.BreakRule;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateBreakRule extends JFrame {
    public UpdateBreakRule(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("修改违纪记录");
        this.setSize(492,511);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("请输入记录id:");
        label1.setBounds(104, 32, 89, 30);
        JLabel label2 = new JLabel();
        label2.setText("查询结果如下:");
        label2.setBounds(174, 87, 98, 30);
        JLabel label3 = new JLabel();
        label3.setText("id：");
        label3.setBounds(136, 129, 65, 30);
        JLabel label4 = new JLabel();
        label4.setText("学号:");
        label4.setBounds(136, 180, 65, 30);
        JLabel label5 = new JLabel();
        label5.setText("姓名:");
        label5.setBounds(138, 231, 65, 30);
        JLabel label6 = new JLabel();
        label6.setText("违纪情况:");
        label6.setBounds(137, 284, 65, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(210, 31, 120, 30);
        JTextField field2 = new JTextField();
        field2.setText("");
        field2.setEnabled(false);
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(213, 130, 120, 30);
        JTextField field3 = new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(213, 181, 120, 30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(213, 233, 120, 30);
        JTextArea field5 = new JTextArea();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(212, 283, 179, 87);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("查询");
        button1.setBounds(350, 34, 95, 27);
        JButton button2 = new JButton();
        button2.setText("修改");
        button2.setEnabled(false);
        button2.setBounds(116, 398, 95, 27);
        JButton button3 = new JButton();
        button3.setText("退出");
        button3.setBounds(278, 398, 95, 27);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = field1.getText();
                BreakRuleService service = new BreakRuleServiceImpl();
                BreakRule rule = service.selectBreakRuleById(Integer.parseInt(id));
                JOptionPane.showMessageDialog(new JFrame(), "查询结果如下");
                field2.setText(String.valueOf(rule.getId()));
                field3.setText(rule.getStunum());
                field4.setText(rule.getName());
                field5.setText(rule.getDisReason());
                button2.setEnabled(true);

            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String updateID = field1.getText();
                BreakRule br = new BreakRule();
                br.setStunum(field3.getText());
                br.setName(field4.getText());
                br.setDisReason(field5.getText());
                BreakRuleService service = new BreakRuleServiceImpl();
                service.UpdateBreakRule(br,Integer.parseInt(updateID));
                JOptionPane.showMessageDialog(new JFrame(),"修改成功！");

            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
